package lium.lbs.lbsim.fragments

import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.buz.hjcdriver.bean.MessageBean
import kotlinx.android.synthetic.main.fragment_messagelist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.MainActivity
import lium.lbs.lbsim.adapter.MessageListAdapter
import lium.lbs.lbsim.utils.IAlertDialog
import lium.lbs.lbsim.utils.MsgMenuPopWindow


class MsgListFragment: BaseFragment() {
    override fun getLayoutId(): Int { return R.layout.fragment_messagelist}
    lateinit var adapter:MessageListAdapter
    companion object {
        fun newInstance(): MsgListFragment {
            return MsgListFragment()
        }
    }
    override fun initView() {
        initRecyclerView()
        addOnClickListeners(R.id.msglist_clear)
    }
    fun initRecyclerView(){
        adapter = MessageListAdapter()
        val layoutManager = LinearLayoutManager(activity)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter.setHeaderView(layoutInflater.inflate(R.layout.fragment_messagelist_headview,null,false))
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        getDataList()
    }


    private fun getDataList() {
        var list = arrayListOf<MessageBean>()
        list.add(MessageBean("系统通知","","xx申请加入群",MessageBean.MSG_SYSTEM))
        list.add(MessageBean("同城交友群","","有出来玩的吗？",MessageBean.MSG_GROUP))
        list.add(MessageBean("好友xx","","呵呵干嘛去洗澡",MessageBean.MSG_FRIENDS))
        adapter.data.addAll(list)
        adapter.notifyDataSetChanged()
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.msglist_clear -> {
                showpop()
                popmenu!!.showPopupWindow(msglist_clear)
            }
        }
    }
    var popmenu: MsgMenuPopWindow?=null
    private fun showpop(){
        if (popmenu==null){
            popmenu = MsgMenuPopWindow(activity as MainActivity)
            popmenu!!.contentView.findViewById<TextView>(R.id.allread).setOnClickListener {
                popReadAll()
                popmenu!!.dismiss()
            }
            popmenu!!.contentView.findViewById<TextView>(R.id.allclear).setOnClickListener {
                popClear()
                popmenu!!.dismiss()
            }
        }
    }

    private fun popReadAll() {
        var dialog = IAlertDialog(baseActivity, IAlertDialog.LayoutStyle.READALL, Gravity.CENTER)
        dialog.setPositiveOnClickListener { _, _ ->
            dialog.dismiss()
        }
        dialog.show()
    }

    private fun popClear() {
        var dialog = IAlertDialog(baseActivity, IAlertDialog.LayoutStyle.CLEARALL, Gravity.CENTER)
        dialog.setPositiveOnClickListener { _, _ ->
            dialog.dismiss()
        }
        dialog.show()
    }

}